package com.ham.mud.commands.spells.damage;

import com.ham.mud.ServerConnection;
import com.ham.mud.TargetableService;
import com.ham.mud.characters.MudCharacter;
import com.ham.mud.characters.player.Player;
import com.ham.mud.commands.spells.Spell;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;
import com.ham.mud.state.CombatState;
import com.ham.mud.state.PVPState;

/**
 * Created by hlucas on Jul 6, 2011 at 10:51:39 AM
 */
public abstract class DamagingSpell extends Spell {
    @Override
    public void castSpell(ServerConnection connection, String target) {
        Player player = connection.getPlayer();
        int mana = player.getVitals().getMana();
        int manaCost = getManaCost();
        double damage = getDamageAmount(player);
        int damageAmount = (int) (damage * player.getIntelligenceModifier());
        if(mana < manaCost) {
            connection.printSolo("You do not have enough mana to cast that spell.");
            return;
        }

        Room room = RoomService.getRoom(player.getZone(), player.getAtX(), player.getAtY());
        if(target == null || target.equals("")) {
            MudCharacter targetCharacter = null;
            if(connection.getState() instanceof PVPState) {
                targetCharacter = ((PVPState)connection.getState()).getEnemy().getPlayer();
            } else if(connection.getState() instanceof CombatState) {
                targetCharacter = ((CombatState)connection.getState()).getCombatWith();
            }

            if(targetCharacter == null) {
                connection.printSolo("Cast that spell upon whom?");
                return;
            }

            player.getVitals().decrementMana(manaCost);
            targetCharacter.getVitals().decrementHealth(damageAmount);
            connection.println(getPlayerText(targetCharacter, damage));
        } else {
            Player targetPlayer = (Player) TargetableService.findTarget(target, room.getOtherPlayers(connection));
            if(targetPlayer == null) {
                connection.printSolo("You do not see that person here.");
                return;
            }
            player.getVitals().decrementMana(manaCost);
            targetPlayer.getVitals().incrementMove(damageAmount);
            connection.println(getPlayerText(targetPlayer, damage));
            room.printToOthers(getOtherPlayerText(player.getName()), connection);
        }
    }

    protected abstract String getPlayerText(MudCharacter target, double damage);
    protected abstract String getOtherPlayerText(String name);
    protected abstract double getDamageAmount(Player player);
}
